package br.inf.solutions.apresentacoes.ssp.model.hierarchy;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import br.com.vexillum.model.annotations.Validate;
import br.com.vexillum.vexhierarchia.model.HierarchyEntity;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.items.FunctionPerson;

@SuppressWarnings({ "serial" })
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name = "hierarchy_entity")
public class HierarchyStructEntity extends HierarchyEntity<HierarchyStructEntity> {

	@Column(nullable = false)
	@Validate(notNull = true)
	private String name;
	
	@Column(length = 15)
	private String acronym;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@Validate(notNull = true)
	@JoinColumn(name = "type_id", nullable = false)
	private HierarchyStructType type;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "classification_id", nullable = false)
	@Validate(notNull = true)
	private HierarchyClassification classification;
	
//	@ManyToOne(fetch = FetchType.LAZY)
//	@JoinColumn(name = "model_id", nullable = true)
//	private ModelName model;

	@Cascade(CascadeType.ALL)
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "hierarchyStructEntity", targetEntity = FunctionPerson.class)
	private List<FunctionPerson> functionPersons;

	public String getInactiveStyleClass() {
		return "incactive-hierarchy";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		if (name != null) {
			this.name = name.toUpperCase();
		} else
			this.name = name;
	}

	public String getAcronym() {
		return acronym;
	}

	public void setAcronym(String acronym) {
		this.acronym = acronym;
	}

	public HierarchyStructType getType() {
		return type;
	}

	public void setType(HierarchyStructType type) {
		this.type = type;
	}

	@Override
	public String getStyleClass() {
		return type.getStyleClass();
	}

	@Override
	public Integer getLevel() {
		return type.getLevel();
	}

	public HierarchyClassification getClassification() {
		return classification;
	}

	public void setClassification(HierarchyClassification classification) {
		this.classification = classification;
	}

	public List<FunctionPerson> getFunctionPersons() {
		return functionPersons;
	}

	public void setFunctionPersons(List<FunctionPerson> functionPersons) {
		this.functionPersons = functionPersons;
	}

	@Override
	public String toString() {
		String namePattern = getPattern() == null ? null : getPattern().getName();
		String thisName = this.getName();
		return namePattern != null ? namePattern + " - " + thisName : thisName;
	}
	
	@Override
	public boolean equals(Object obj) {
		return super.equals(obj);
	}

}